import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:my_desk_flutter/controller/auth_controller.dart';
import 'package:my_desk_flutter/controller/person_provider.dart';
import 'package:my_desk_flutter/model/person_info.dart';
import 'package:my_desk_flutter/widget/container_extision.dart';

class MinePage extends ConsumerWidget {
  const MinePage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final personInfo = ref.watch(personInfoProvider).value;
    final isLogin = ref.watch(authControllerProvider).value?.isAuth ?? false;
    return Scaffold(
      // appBar: AppBar(
      //   title: const Text("我的"),
      // ),
      body: SafeArea(
        child: Column(
          children: [
            if (!isLogin)
              GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, '/login');
                },
                child: Text(
                  "去登录",
                  style: Theme.of(context).textTheme.titleLarge,
                ),
              ),
            if (personInfo != null) _buildPersonInfo(context, personInfo),
          ],
        ).paddingSafeArea(),
      ),
      floatingActionButton: Visibility(
        visible: isLogin,
        child: FloatingActionButton(
          child: const Icon(Icons.exit_to_app_rounded),
          onPressed: () {
            ref.read(authControllerProvider.notifier).logout();
          },
        ),
      ),
    );
  }

  Widget _buildPersonInfo(BuildContext context, PersonInfo personInfo) {
    return Text(
      personInfo.userInfo?.nickname ?? '',
      style: Theme.of(context).textTheme.titleLarge,
    );
  }
}
